package handlers

import (
	"ecom-admin/cache"
	"ecom-admin/models"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"strconv"
	"time"
)

func GoodAdd(c *gin.Context) {
	good_code := c.PostForm("good_code")
	good_name := c.PostForm("good_name")
	good_price, _ := strconv.Atoi(c.PostForm("good_price"))
	if good_code == "" || good_name == "" || good_price == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Good{
		Model:     gorm.Model{},
		GoodCode:  good_code,
		GoodName:  good_name,
		GoodPrice: good_price,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func GoodOrderAdd(c *gin.Context) {
	order_code := c.PostForm("order_code")
	order_price, _ := strconv.Atoi(c.PostForm("order_price"))
	gid, _ := strconv.Atoi(c.PostForm("gid"))
	if order_code == "" || order_price == 0 || gid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Order{
		Model:      gorm.Model{},
		OrderCode:  order_code,
		OrderPrice: order_price,
		Gid:        gid,
	}
	err := models.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func GoodOrderList(c *gin.Context) {
	/*
		SELECT
		  orders.id AS OrderId,
		  orders.order_code AS OrderCode,
		  orders.order_price AS OrderPrice,
		  goods.good_code AS GoodCode,
		  goods.good_name AS GoodName,
		  goods.good_price AS GoodPrice
		FROM
		  orders
		  LEFT JOIN goods ON orders.gid = goods.id
	*/
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var goodOrder []models.GoodOrderList
	redis_key := "good_order_list_page_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select("orders.id AS OrderId",
				"orders.order_code AS OrderCode",
				"orders.order_price AS OrderPrice",
				"goods.good_code AS GoodCode",
				"goods.good_name AS GoodName",
				"goods.good_price AS GoodPrice").
			Joins("LEFT JOIN goods ON orders.gid = goods.id").
			Offset(offset).Limit(size).Find(&goodOrder)
		jsonData, _ := json.Marshal(goodOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &goodOrder)
	}
	var total int64
	models.DB.Model(&models.Order{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  goodOrder,
		"total": total,
	})
	return
}

func GoodOrderDetail(c *gin.Context) {
	/*
		SELECT
		  orders.id AS OrderId,
		  orders.order_code AS OrderCode,
		  orders.order_price AS OrderPrice,
		  goods.good_code AS GoodCode,
		  goods.good_name AS GoodName,
		  goods.good_price AS GoodPrice
		FROM
		  orders
		  LEFT JOIN goods ON orders.gid = goods.id
	*/
	id := c.Query("id")
	var goodOrder models.GoodOrderList
	redis_key := "good_order_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("orders").
			Select("orders.id AS OrderId",
				"orders.order_code AS OrderCode",
				"orders.order_price AS OrderPrice",
				"goods.good_code AS GoodCode",
				"goods.good_name AS GoodName",
				"goods.good_price AS GoodPrice").
			Joins("LEFT JOIN goods ON orders.gid = goods.id").
			Where("orders.id = ?", id).Find(&goodOrder)
		jsonData, _ := json.Marshal(goodOrder)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &goodOrder)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": goodOrder,
	})
	return
}
